/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ru.ruchin.tek.employees.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author yruchin
 */
public class EmployeesServlet extends HttpServlet {
    private static final Map<String, QueryHandler> handlerRegistry;

    static {
        handlerRegistry = new HashMap<String, QueryHandler>();
        handlerRegistry.put("/getEmployeeInfo", new EmployeeInfoQueryHandler());
        handlerRegistry.put("/getDepartmentInfo", new DepartmentInfoQueryHandler());
        handlerRegistry.put("/listEmployees", new EmployeeListQueryHandler());
        handlerRegistry.put("/listDepartments", new DepartmentListQueryHandler());
        handlerRegistry.put("/fillDatabase", new FillDatabaseHandler());
        handlerRegistry.put("/removeObject", new RemoveObjectQueryHandler());
        handlerRegistry.put("/saveObject", new SaveObjectQueryHandler());
    }
            
    public EmployeesServlet() {
        super();
    }

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String path = request.getServletPath();
        QueryHandler qh = handlerRegistry.get(path);

        if (qh == null) {
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }

        String responseBody = qh.handleQuery(request.getParameterMap(), getServletContext());
        
        response.setContentType("application/json;charset=UTF-8");
        
        PrintWriter out = response.getWriter();
        try {
            out.write(responseBody);
            out.flush();
        } finally {
            out.close();
        }
    }

    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }
}
